package com.algo.aldi;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

public class SimpleJob3_2 implements Job {

    public static void main_1(String[] args) throws SchedulerException, InterruptedException {
        task();
    }

    public static void main(String[] args) throws SchedulerException, InterruptedException {

        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        JobDetail job = newJob(SimpleJob3_2.class)
                .withIdentity("job1", "group1")
                .build();

        CronTrigger trigger = newTrigger()
                .withIdentity("trigger1", "group1")
//                .withSchedule(cronSchedule("0 0/20 * * * ?"))
                .withSchedule(cronSchedule("0 0 11 * * ? *"))
                .build();

        sched.scheduleJob(job, trigger);

        sched.start();
        TimeUnit.HOURS.sleep(24);
    }

    static int TIMES = 2;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            for (int i = 0; i < TIMES; i++)
                task();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static String url = "https://alq2020.69dp.cn/Api/UserSeckill/addOrder";
    static String ut = "%40%25%5E%2AMd3InnJdxqMDSu1vVlkS%2FEGjbtUe6NSQL9wrIXl3S6k%3D";
    static RestTemplate restTemplate = new RestTemplate();
    static HttpEntity<MultiValueMap<String, String>> requestb = null;

    static {
        HttpHeaders headers = new HttpHeaders();
        //设置为form方式
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("ut", ut);
        map.add("userid", ut);
        map.add("goods_id", "53");
        map.add("answer", "会员秒杀");
        requestb = new HttpEntity<>(map, headers);
    }
    private static void task() {
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(url, requestb, String.class);
            System.out.println(response.getBody());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
